package com.b1502.store2.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.b1502.store2.Bean.CategoryListBean;
import com.b1502.store2.R;
import com.b1502.store2.adapter.CategroyListAdapter;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 分类列表页面
 * <p>
 * TabLayout
 */
public class CategoryActivity extends BaseActivity {

    private String userName;
    private String userId;

    private RecyclerView fl_list_rv_;
    private CategroyListAdapter catelistadapter;
    private List<CategoryListBean.ProductsBean> categroylist;

    public static void open(Activity activity) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        initView();
        recycle();
        getCategory("categoryId","b0066438-515e-4422-982a-ca203b35ea1c");
        getProducts(userId,0);
    }

    private void initView() {
        fl_list_rv_=(RecyclerView)findViewById(R.id.fl_list_rv);
        Intent intent=getIntent();
        userName = intent.getStringExtra("UserName");
        userId = intent.getStringExtra("UserId");
        initStoreBar(userName);


    }

    private void recycle() {



        //RecyclerView需要配置
        //设置排列方式
        StaggeredGridLayoutManager layout=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //设置布局管理器
        fl_list_rv_.setLayoutManager(layout);
        //添加动画,移除动画
        fl_list_rv_.setItemAnimator(new DefaultItemAnimator());
        //设置分割线
        fl_list_rv_.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //设置分割线大小
                outRect.set(5,5,5,5);
            }
        });

    }

    public void getCategory(String param, String categoryId){
        StoreParams params = new StoreParams(UrlUtil.GetCategory,param,categoryId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Log.d(TAG, result);




            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void getProducts(String productCategoryId, int startRowIndex) {
        StoreParams params = new StoreParams(UrlUtil.GetProducts, productCategoryId, startRowIndex);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Log.d(TAG, result);

                Gson gson=new Gson();

                CategoryListBean categoryListBeen = gson.fromJson(result, CategoryListBean.class);
                categroylist =categoryListBeen.getProducts();
                catelistadapter = new CategroyListAdapter(CategoryActivity.this,categroylist);
                fl_list_rv_.setAdapter(catelistadapter);

                catelistadapter.setOnclick(new CategroyListAdapter.addOncli() {
                    @Override
                    public void onItemClick(View view, CategoryListBean.ProductsBean data) {

                    }

                    @Override
                    public void onLongClick(View view, CategoryListBean.ProductsBean data) {

                    }
                });

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
